home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / IFF / Old_IFF_Packages / July_1992 / modules / saveilbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-06  |  4.9 KB  |  174 lines

  1. /* saveilbm.c 04/92  C. Scheppner CBM
  2.  *
  3.  * High-level ILBM save routines
  4.  *
  5.  * 37.10 07/92 - use scr->RastPort.BitMap instead of &scr->BitMap
  6.  *            for future compatibility
  7.  */
  8. #define INTUI_V36_NAMES_ONLY
  9.  
  10. #include "iffp/ilbm.h"
  11. #include "iffp/ilbmapp.h"
  12.  
  13. extern struct Library *GfxBase;
  14.  
  15. /* screensave.c
  16.  *
  17.  * Given an ILBMInfo with a  currently available (not in use)
  18.  *   ParseInfo->iff IFFHandle, a screen pointer, filename, and
  19.  *   optional chunklist, will save screen as an ILBM
  20.  * The struct Chunk *chunklist1 and 2 are for chunks you wish written
  21.  * out other than BMHD, CMAP, and CAMG (they will be screened out
  22.  * because they are computed and written separately).
  23.  *
  24.  * Note -  screensave passes NULL for transparent color and mask
  25.  *
  26.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  27.  */
  28. LONG screensave(struct ILBMInfo *ilbm,
  29.             struct Screen *scr,
  30.             struct Chunk *chunklist1, struct Chunk *chunklist2,
  31.             UBYTE *filename)
  32. {
  33. extern struct Library *GfxBase;
  34. UWORD *colortable, count;
  35. ULONG modeid;
  36. LONG error;
  37. int k;
  38.  
  39.     if(GfxBase->lib_Version >= 36)
  40.     modeid=GetVPModeID(&scr->ViewPort);
  41.     else
  42.     modeid = scr->ViewPort.Modes & OLDCAMGMASK;
  43.  
  44.     count = scr->ViewPort.ColorMap->Count;
  45.     if(colortable = (UWORD *)AllocMem(count << 1, MEMF_CLEAR))
  46.     {
  47.     for(k=0; k<count; k++)    colortable[k]=GetRGB4(scr->ViewPort.ColorMap,k);
  48.  
  49.         error = saveilbm(ilbm, scr->RastPort.BitMap, modeid,
  50.         scr->Width, scr->Height, scr->Width, scr->Height,
  51.         colortable, count, 4,
  52.         mskNone, 0,
  53.         chunklist1, chunklist2, filename);
  54.     FreeMem(colortable,count << 1);
  55.     }
  56.     else error = IFFERR_NOMEM;
  57.     return(error);
  58. }
  59.  
  60.  
  61. /* saveilbm
  62.  *
  63.  * Given an ILBMInfo with a currently available (not-in-use)
  64.  *   ParseInfo->iff IFFHandle, a BitMap ptr,
  65.  *   modeid, widths/heights, colortable, ncolors, bitspergun,
  66.  *   masking, transparent color, optional chunklists, and filename,
  67.  *   will save the bitmap as an ILBM.
  68.  *
  69.  *  if bitspergun=4,  colortable is words, each with nibbles 0RGB
  70.  *  if bitspergun=8,  colortable is byte guns of RGBRGB etc. (like a CMAP)
  71.  *  if bitspergun=32, colortable is ULONG guns of RGBRGB etc.
  72.  *     Only the high eight bits of each gun will be written to CMAP.
  73.  *     Four bit guns n will be saved as nn
  74.  *
  75.  * The struct Chunk *chunklist is for chunks you wish written
  76.  * other than BMHD, CMAP, and CAMG (they will be screened out)
  77.  * because they are calculated and written separately
  78.  *
  79.  * Returns 0 for success, or an IFFERR
  80.  */
  81. LONG saveilbm(struct ILBMInfo *ilbm,
  82.         struct BitMap *bitmap, ULONG modeid,
  83.         WORD width, WORD height, WORD pagewidth, WORD pageheight,
  84.         APTR colortable, UWORD ncolors, UWORD bitspergun,
  85.         WORD masking, WORD transparentColor,
  86.         struct Chunk *chunklist1, struct Chunk *chunklist2,
  87.         UBYTE *filename)
  88. {
  89. struct IFFHandle *iff;
  90. struct Chunk *chunk;
  91. ULONG chunkID;
  92. UBYTE *bodybuf;
  93. LONG size, error = 0L;
  94. #define BODYBUFSZ    4096
  95.  
  96.     iff = ilbm->ParseInfo.iff;
  97.  
  98.     if(!(modeid & 0xFFFF0000))    modeid &= OLDCAMGMASK;
  99.  
  100.     if(!(bodybuf = AllocMem(BODYBUFSZ,MEMF_PUBLIC)))
  101.     {
  102.     message(SI(MSG_IFFP_NOMEM));
  103.     return(IFFERR_NOMEM);
  104.     }
  105.  
  106.     if(!(error = openifile(ilbm, filename, IFFF_WRITE)))
  107.     {
  108.     D(bug("Opened %s for write\n",filename));
  109.  
  110.     error = PushChunk(iff, ID_ILBM, ID_FORM, IFFSIZE_UNKNOWN);
  111.  
  112.     D(bug("After PushChunk FORM ILBM - error = %ld\n", error));
  113.  
  114.         initbmhd(&ilbm->Bmhd, bitmap, masking, cmpByteRun1, transparentColor,
  115.                     width, height, pagewidth, pageheight, modeid);
  116.  
  117.     D(bug("Error before putbmhd = %ld\n",error));
  118.  
  119.     CkErr(putbmhd(iff,&ilbm->Bmhd));    
  120.  
  121.     if(colortable)    CkErr(putcmap(iff,colortable,ncolors,bitspergun));
  122.  
  123.     ilbm->camg = modeid;
  124.     D(bug("before putcamg - error = %ld\n",error));
  125.     CkErr(putcamg(iff,&modeid));
  126.  
  127.     D(bug("Past putBMHD, CMAP, CAMG - error = %ld\n",error));
  128.  
  129.     /* Write out chunklists 1 & 2 (if any), except for
  130.      * any BMHD, CMAP, or CAMG (computed/written separately)
  131.      */
  132.     for(chunk = chunklist1; chunk; chunk = chunk->ch_Next)
  133.         {
  134.         D(bug("chunklist1 - have a %.4s\n",&chunk->ch_ID));
  135.         chunkID = chunk->ch_ID;
  136.         if((chunkID != ID_BMHD)&&(chunkID != ID_CMAP)&&(chunkID != ID_CAMG))
  137.         {
  138.         size = chunk->ch_Size==IFFSIZE_UNKNOWN ?
  139.             strlen(chunk->ch_Data) : chunk->ch_Size;
  140.         D(bug("Putting %.4s\n",&chunk->ch_ID));
  141.         CkErr(PutCk(iff, chunkID, size, chunk->ch_Data));
  142.         }
  143.         }
  144.  
  145.     for(chunk = chunklist2; chunk; chunk = chunk->ch_Next)
  146.         {
  147.         chunkID = chunk->ch_ID;
  148.         D(bug("chunklist2 - have a %.4s\n",&chunk->ch_ID));
  149.         if((chunkID != ID_BMHD)&&(chunkID != ID_CMAP)&&(chunkID != ID_CAMG))
  150.         {
  151.         size = chunk->ch_Size==IFFSIZE_UNKNOWN ?
  152.             strlen(chunk->ch_Data) : chunk->ch_Size;
  153.         D(bug("Putting %.4s\n",&chunk->ch_ID));
  154.         CkErr(PutCk(iff, chunkID, size, chunk->ch_Data));
  155.         }
  156.         }
  157.  
  158.     /* Write out the BODY
  159.      */
  160.     CkErr(putbody(iff, bitmap, NULL, &ilbm->Bmhd, bodybuf, BODYBUFSZ));
  161.  
  162.     D(bug("Past putbody - error = %ld\n",error));
  163.  
  164.  
  165.     CkErr(PopChunk(iff));    /* close out the FORM */
  166.     closeifile(ilbm);    /* and the file */
  167.     }
  168.  
  169.     FreeMem(bodybuf,BODYBUFSZ);
  170.  
  171.     return(error);
  172. }
  173.  
  174.